// Copyright (c) 2023-2024 FlyByWire Simulations
//
// SPDX-License-Identifier: GPL-3.0

import {
  AircraftConfig,
  EngineModelParameters,
  FlightModelParameters,
  FMSymbolsConfig,
  LnavConfig,
  VnavConfig,
  VnavDescentMode,
} from '@fmgc/flightplanning/AircraftConfigTypes';
import { FlapConf } from '@fmgc/guidance/vnav/common';

const lnavConfig: LnavConfig = {
  DEFAULT_MIN_PREDICTED_TAS: 160,
  TURN_RADIUS_FACTOR: 1.0,
  NUM_COMPUTED_TRANSITIONS_AFTER_ACTIVE: -1,
  EMIT_END_OF_VD_MARKER: true,
};

const vnavConfig: VnavConfig = {
  VNAV_DESCENT_MODE: VnavDescentMode.NORMAL,
  VNAV_EMIT_CDA_FLAP_PWP: false,
  VNAV_USE_LATCHED_DESCENT_MODE: false,
  IDLE_N1_MARGIN: 3,
  MAXIMUM_FUEL_ESTIMATE: 250_000,
  LIM_PSEUDO_WPT_LABEL: '(SPDLIM)',
  VMO: 340,
  MMO: 0.89,
};

const flightModelParams: FlightModelParameters = {
  wingSpan: 261.65,
  wingArea: 9096,
  wingEffcyFactor: 0.7,
  requiredAccelRateKNS: 1.33,
  requiredAccelRateMS2: 0.684,
  gravityConstKNS: 19.0626,
  gravityConstMS2: 9.806665,
  machValues: [0, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0],
  dragCoefficientCorrections: [0, 0, 0, 0.00012, 0.00026, 0.0004, 0.00061, 0.001, 0.0018, 0.0031, 0.005, 0.0082],
  speedBrakeDrag: 0.01154,
  gearDrag: 0.019,
  dragPolarCoefficients: {
    [FlapConf.CLEAN]: [0.0236, -0.0167, 0.0171, 0.0291],
    [FlapConf.CONF_1]: [0.0305, -0.02, 0.0285, 0.0105],
    [FlapConf.CONF_2]: [0.1222, -0.0017, -0.0073, 0.0169],
    [FlapConf.CONF_3]: [0.1529, -0.0308, 0.0127, 0.0026],
    [FlapConf.CONF_FULL]: [0.1628, -0.0193, 0.0013, 0.0048],
  },
};

const engineModelParams: EngineModelParameters = {
  maxThrust: 80_213,
  numberOfEngines: 4,
  fuelBurnFactor: 2.8,
  cn1ClimbLimit: [
    [-2000, 30.8, 56.87, 80.28, 72.0],
    [2000, 20.99, 48.157, 82.58, 74.159],
    [5000, 16.139, 43.216, 84.642, 75.737],
    [8000, 7.342, 38.17, 86.835, 77.338],
    [10000, 4.051, 34.518, 88.183, 77.999],
    [10000.1, 4.051, 34.518, 87.453, 77.353],
    [12000, 0.76, 30.865, 88.303, 78.66],
    [15000, -4.859, 25.039, 89.748, 79.816],
    [17000, -9.934, 19.813, 90.668, 80.895],
    [20000, -15.822, 13.676, 92.106, 81.894],
    [24000, -22.75, 6.371, 94.588, 83.543],
    [27000, -29.105, -0.304, 96.203, 85.358],
    [29314, -32.049, -3.377, 96.82, 85.906],
    [31000, -34.98, -6.452, 98.568, 86.909],
    [35000, -45.679, -17.15, 100.977, 89.57],
    [39000, -45.679, -17.15, 103.085, 90.377],
    [41500, -45.679, -17.15, 104.509, 91.476],
  ],
  /**
   * Table 1502 - CN2 vs CN1 @ Mach 0, 0.2, 0.9
   * n2_to_n1_table
   * @param i row index (n2)
   * @param j 1 = Mach 0, 2 = Mach 0.2, 3 = Mach 0.9
   * @returns Corrected N1 (CN1)
   */
  table1502: [
    [0, 0, 0.2, 0.9],
    [18.2, 0.0, 0.0, 17.0],
    [22.0, 1.9, 1.9, 17.4],
    [26.0, 2.5, 2.5, 18.2],
    [57.0, 12.8, 12.8, 27.0],
    [68.2, 19.6, 19.6, 34.827774],
    [77.0, 26.0, 26.0, 40.839552],
    [83.0, 31.42024, 31.42024, 44.768766],
    [89.0, 40.972041, 40.972041, 50.09214],
    [92.8, 51.0, 51.0, 55.042],
    [97.0, 65.0, 65.0, 65.0],
    [100.0, 77.0, 77.0, 77.0],
    [104.0, 85.0, 85.0, 85.5],
    [116.5, 101.0, 101.0, 101.0],
  ],
  /**
   * Table 1503 - Turbine LoMach (0) CN2 vs. Throttle @ IAP Ratio 1.00000000, 1.20172257, 1.453783983, 2.175007333, 3.364755652, 4.47246108, 5.415178313
   * mach_0_corrected_commanded_ne_table
   * @param i row index (thrust lever position)
   * @param j IAP ratio
   * @returns Corrected N2 (CN2)
   */
  table1503: [
    [0, 1.0, 1.20172257, 1.453783983, 2.175007333, 3.364755652, 4.47246108, 5.415178313],
    [0.0, 68.2, 69.402657, 70.671269, 73.432244, 76.544349, 78.644882, 78.644882],
    [0.1, 76.0, 77.340205, 78.753906, 81.830654, 85.298688, 87.639458, 87.639458],
    [0.2, 83.0, 84.463645, 86.007556, 89.367688, 93.155146, 95.711513, 95.711513],
    [0.4, 92.8, 94.436461, 96.162664, 99.919535, 104.154188, 107.01239, 107.01239],
    [0.6, 98.0, 99.728159, 101.55109, 105.518475, 109.990414, 113.008774, 113.008774],
    [0.75, 101.5, 103.289879, 105.177914, 109.286991, 113.918643, 117.044802, 117.044802],
    [0.9, 103.0, 104.81633, 106.0, 110.90207, 115.60217, 118.774528, 118.774528],
    [1.0, 104.2, 106.037491, 107.97575, 112.194133, 116.948991, 120.158309, 120.158309],
  ],
  /**
   * Table 1504 - Turbine HiMach (0.9) CN2 vs. Throttle @ IAP Ratio 1.00000000, 1.20172257, 1.453783983, 2.175007333, 3.364755652, 4.47246108, 5.415178313
   * mach_hi_corrected_commanded_ne_table
   * @param i row index (thrust lever position)
   * @param j IAP ratio
   * @returns Corrected N2 (CN2)
   */
  table1504: [
    [0, 1.0, 1.20172257, 1.453783983, 2.175007333, 3.364755652, 4.47246108, 5.415178313],
    [0.0, 63.267593, 64.383271, 65.560133, 68.121427, 71.008456, 72.957073, 72.957073],
    [0.1, 70.503476, 71.746753, 73.058212, 75.912441, 79.129658, 81.301137, 81.301137],
    [0.2, 76.997217, 78.355007, 79.787258, 82.904376, 86.417916, 88.789399, 88.789399],
    [0.4, 86.088455, 87.606562, 89.207922, 92.693086, 96.621477, 99.272967, 99.272967],
    [0.6, 90.912377, 92.51555, 94.206642, 97.887095, 102.035612, 104.835676, 104.835676],
    [0.75, 94.159247, 95.819677, 97.571165, 101.383063, 105.679741, 108.579808, 108.579808],
    [0.9, 95.550763, 97.235732, 98.333795, 102.881334, 107.24151, 110.184435, 110.184435],
    [1.0, 104.2, 106.037491, 107.97575, 112.194133, 116.948991, 120.158309, 120.158309],
  ],
  /**
   * Table 1506 - Corrected net Thrust vs CN1 @ Mach 0 to 0.9 in 0.1 steps
   * n1_and_mach_on_thrust_table
   * @param i row index (CN1)
   * @param j mach
   * @returns Corrected net thrust (pounds of force)
   */
  table1506: [
    [0, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
    [0, 0.015, -0.15, 0, 0, 0, 0, 0, 0, 0, 0],
    [20, 0.1286, -0.008, 0.015, 0.008, -0.023, -0.061, -0.081, -0.135, -0.21, -0.3],
    [25, 0.155, 0.0236, 0.018, 0.021, -0.0145, -0.038, -0.08, -0.106, -0.15, -0.17],
    [30.0, 0.16, 0.16, 0.11, 0.1, 0.08, 0.03, -0.01, -0.044, -0.064, -0.088],
    [35.0, 0.315, 0.151, 0.123, 0.115, 0.1, 0.053, 0.028, 0.006, -0.008, -0.046],
    [40.0, 0.396, 0.195, 0.149, 0.07, 0.065, 0.051, 0.042, 0.021, 0.015, -0.038],
    [45.0, 0.472, 0.3, 0.188, 0.114, 0.08, 0.076, 0.07, 0.032, 0.021, -0.021],
    [50.0, 0.543, 0.373, 0.254, 0.14, 0.109, 0.16, 0.11, 0.033, 0.026, -0.015],
    [55.0, 0.611, 0.448, 0.324, 0.18, 0.145, 0.19, 0.175, 0.085, 0.055, 0.011],
    [60.0, 0.714, 0.528, 0.402, 0.25, 0.182, 0.225, 0.22, 0.12, 0.124, 0.024],
    [65.0, 0.792, 0.615, 0.489, 0.32, 0.218, 0.236, 0.275, 0.195, 0.13, 0.03],
    [70.0, 0.88, 0.712, 0.586, 0.38, 0.28, 0.25, 0.32, 0.285, 0.17, 0.1],
    [75.0, 0.982, 0.821, 0.695, 0.43, 0.39, 0.3, 0.33, 0.39, 0.32, 0.19],
    [80.0, 1.092, 0.941, 0.811, 0.55, 0.45, 0.4, 0.405, 0.46, 0.38, 0.28],
    [85.0, 1.23, 1.07, 0.932, 0.66, 0.57, 0.48, 0.5, 0.43, 0.44, 0.38],
    [90.0, 1.388, 1.206, 1.049, 0.75, 0.68, 0.56, 0.54, 0.5, 0.48, 0.4],
    [95.0, 1.538, 1.34, 1.153, 0.875, 0.75, 0.6, 0.57, 0.56, 0.66, 0.69],
    [100.0, 1.603, 1.465, 1.229, 0.95, 0.8, 0.7, 0.74, 0.74, 0.68, 0.75],
    [105.0, 1.655, 1.488, 1.258, 1.03, 0.859, 0.8, 0.82, 0.77, 0.69, 0.77],
    [110.0, 1.671, 1.503, 1.283, 1.1, 0.935, 0.9, 0.92, 0.8, 0.71, 0.78],
  ],
};

const fmsSymbolConfig: FMSymbolsConfig = {
  publishDepartureIdent: true,
  showRnpArLabel: true,
};

export const A380AircraftConfig: AircraftConfig = {
  lnavConfig,
  vnavConfig,
  engineModelParameters: engineModelParams,
  flightModelParameters: flightModelParams,
  fmSymbolConfig: fmsSymbolConfig,
};
